#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
typedef struct oper
{
    char op;
    char* arr;
}ope;

#define BUF_SIZE 1024
int main(){
    //创建套接字
    int serv_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    //将套接字和IP、端口绑定
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //每个字节都用0填充
    serv_addr.sin_family = AF_INET;  //使用IPv4地址
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //具体的IP地址
    serv_addr.sin_port = htons(1234);  //端口
    bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    //进入监听状态，等待用户发起请求
    listen(serv_sock, 20);
    //接收客户端请求
    struct sockaddr_in clnt_addr;
    int str_len;
    socklen_t clnt_addr_size = sizeof(clnt_addr);
    char op;
    char *arr;

    

    int nums;
    double sum = 0;
    ope o;

    char ak[10]; 
    o.arr = ak;
    o.op = 'n';


    printf("ready to accept!\n");
    int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
    printf("ssss\n");
    printf("%c",&o.op);
    read(clnt_sock,&o,sizeof(o));
    printf("ssss\n");
    op = o.op;
    nums = o.arr[0]-'0';
    arr = o.arr;
    printf("%c\n",o.op);
    for(int i = 0;i < nums ;i++)
    {
        double temp = arr[i]-'0';
        if(op == '+') sum+=temp;
        if(op == '-') sum-=temp;
        if(op == '*') sum*=temp;
        if(op == '/') sum/=temp;
    }
    write(clnt_sock,&sum,sizeof(sum));
    close(serv_sock);
    return 0;
}